{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6e2bee34",
   "metadata": {},
   "source": [
    "# Python 条件语句\n",
    "## If\n",
    "Python 支持数学中常见的逻辑条件：\n",
    "\n",
    "等于：a == b\n",
    "\n",
    "不等于：a != b\n",
    "\n",
    "小于：a < b\n",
    "\n",
    "小于或等于：a <= b\n",
    "\n",
    "大于：a > b\n",
    "\n",
    "大于或等于：a >= b\n",
    "\n",
    "这些条件可以以多种方式使用，最常见的是在“if 语句”和循环中。\n",
    "\n",
    "“if 语句”是使用 if 关键字编写的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a72a2b54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b is greater than a\n"
     ]
    }
   ],
   "source": [
    "# 在这个例子中，我们使用了两个变量 a 和 b，它们被用作 if 语句的一部分来测试 b 是否大于 a。 \n",
    "# 由于 a 是 33，b 是 200，我们知道 200 大于 33，所以我们打印到屏幕上“b 大于 a”。\n",
    "a = 33\n",
    "b = 200\n",
    "if b > a:\n",
    "  print(\"b is greater than a\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c07ea7be",
   "metadata": {},
   "source": [
    "## Elif\n",
    "elif 关键字是 python 的说法“如果前面的条件不成立，那么试试这个条件”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "77047909",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a and b are equal\n"
     ]
    }
   ],
   "source": [
    "a = 33\n",
    "b = 33\n",
    "if b > a:\n",
    "  print(\"b is greater than a\")\n",
    "elif a == b:\n",
    "  print(\"a and b are equal\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ee7e94c",
   "metadata": {},
   "source": [
    "## Else\n",
    "else 关键字捕获任何未被上述条件捕获的内容。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4d656170",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is greater than b\n"
     ]
    }
   ],
   "source": [
    "a = 200\n",
    "b = 33\n",
    "if b > a:\n",
    "  print(\"b is greater than a\")\n",
    "elif a == b:\n",
    "  print(\"a and b are equal\")\n",
    "else:\n",
    "  print(\"a is greater than b\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f1205b4",
   "metadata": {},
   "source": [
    "## 简写\n",
    "如果你只有一个语句要执行，你可以把它放在同一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4409e556",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is greater than b\n"
     ]
    }
   ],
   "source": [
    "if a > b: print(\"a is greater than b\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ed88ea52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B\n"
     ]
    }
   ],
   "source": [
    "a = 2\n",
    "b = 330\n",
    "print(\"A\") if a > b else print(\"B\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9088e1f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=\n"
     ]
    }
   ],
   "source": [
    "a = 330\n",
    "b = 330\n",
    "print(\"A\") if a > b else print(\"=\") if a == b else print(\"B\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb333bfa",
   "metadata": {},
   "source": [
    "## 嵌套\n",
    "if 语句中可以有 if 语句，这称为嵌套 if 语句。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "44462e48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Above ten,\n",
      "and also above 20!\n"
     ]
    }
   ],
   "source": [
    "x = 41\n",
    "\n",
    "if x > 10:\n",
    "  print(\"Above ten,\")\n",
    "  if x > 20:\n",
    "    print(\"and also above 20!\")\n",
    "  else:\n",
    "    print(\"but not above 20.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "253fba23",
   "metadata": {},
   "source": [
    "# Python 循环语句\n",
    "Python 有两个原始循环命令：\n",
    "\n",
    "while 循环\n",
    "for 循环\n",
    "\n",
    "## while 循环\n",
    "使用 while 循环，只要条件为真，我们就可以执行一组语句。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cbe3b990",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "while i < 6:\n",
    "  print(i)\n",
    "  i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "614c7cf2",
   "metadata": {},
   "source": [
    "## For 循环\n",
    "for 循环用于迭代序列（即列表、元组、字典、集合或字符串）。\n",
    "\n",
    "这不像其他编程语言中的 for 关键字，而更像是其他面向对象编程语言中的迭代器方法。\n",
    "\n",
    "使用 for 循环，我们可以执行一组语句，对列表、元组、集合等中的每个项目执行一次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ab8c0383",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "for x in fruits:\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4e3df914",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b\n",
      "a\n",
      "n\n",
      "a\n",
      "n\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# 遍历字符串\n",
    "# 即使字符串是可迭代对象，它们也包含一系列字符：\n",
    "for x in \"banana\":\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b9eec21",
   "metadata": {},
   "source": [
    "## 中断语句\n",
    "使用 break 语句，我们可以在循环遍历所有项目之前停止循环："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "860550d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "banana\n"
     ]
    }
   ],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "for x in fruits:\n",
    "  print(x)\n",
    "  if x == \"banana\":\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1cd17c48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "while i < 6:\n",
    "  i += 1\n",
    "  print(i)\n",
    "  if i == 3:\n",
    "    break"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ba34f12",
   "metadata": {},
   "source": [
    "## continue 语句\n",
    "使用 continue 语句，我们可以停止循环的当前迭代，并继续下一个："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "634145bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apple\n",
      "cherry\n"
     ]
    }
   ],
   "source": [
    "fruits = [\"apple\", \"banana\", \"cherry\"]\n",
    "for x in fruits:\n",
    "  if x == \"banana\":\n",
    "    continue\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "dac0fd2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "while i < 6:\n",
    "  i += 1\n",
    "  print(i)\n",
    "  if i == 3:\n",
    "    continue"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ee65b6f",
   "metadata": {},
   "source": [
    "## 循环中的Else\n",
    "循环中的 else 关键字指定循环结束时要执行的代码块："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d2d1b1c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "Finally finished!\n"
     ]
    }
   ],
   "source": [
    "for x in range(6):\n",
    "  print(x)\n",
    "else:\n",
    "  print(\"Finally finished!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "69f28689",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "i is no longer less than 6\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "while i < 6:\n",
    "  print(i)\n",
    "  i += 1\n",
    "else:\n",
    "  print(\"i is no longer less than 6\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b8123576",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "for x in range(6):\n",
    "  if x == 3: break\n",
    "  print(x)\n",
    "else:\n",
    "  print(\"Finally finished!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "953ddb20",
   "metadata": {},
   "source": [
    "## range() 函数\n",
    "要循环一组代码指定的次数，我们可以使用 range() 函数\n",
    "\n",
    "range() 函数返回一个数字序列，默认情况下从 0 开始，递增 1（默认情况下），并以指定的数字结束。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5d3a3931",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for x in range(6):\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a3c29fef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for x in range(2, 6):\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8476eff1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "5\n",
      "8\n",
      "11\n",
      "14\n",
      "17\n",
      "20\n",
      "23\n",
      "26\n",
      "29\n"
     ]
    }
   ],
   "source": [
    "for x in range(2, 30, 3):\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eda9ef7c",
   "metadata": {},
   "source": [
    "# Try Except\n",
    "try 块可让您测试代码块的错误。\n",
    "\n",
    "except 块可让您处理错误。\n",
    "\n",
    "finally 块允许您执行代码，而不管 try- 和 except 块的结果。\n",
    "\n",
    "## 异常处理\n",
    "当发生错误或异常时，Python 通常会停止并生成错误消息。\n",
    "\n",
    "可以使用 try 语句处理这些异常："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "75e0a25a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "  print(z)\n",
    "except:\n",
    "  print(\"An exception occurred\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "365c41eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variable z is not defined\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "  print(z)\n",
    "except NameError:\n",
    "  print(\"Variable z is not defined\")\n",
    "except:\n",
    "  print(\"Something else went wrong\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "98fdf193",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello\n",
      "Nothing went wrong\n"
     ]
    }
   ],
   "source": [
    "# 使用 else 关键字来定义在未引发错误时要执行的代码块：\n",
    "try:\n",
    "  print(\"Hello\")\n",
    "except:\n",
    "  print(\"Something went wrong\")\n",
    "else:\n",
    "  print(\"Nothing went wrong\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "329c630e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29\n",
      "The 'try except' is finished\n"
     ]
    }
   ],
   "source": [
    "# 如果指定了 finally 块，则无论 try 块是否引发错误，都将执行。\n",
    "try:\n",
    "  print(x)\n",
    "except:\n",
    "  print(\"Something went wrong\")\n",
    "finally:\n",
    "  print(\"The 'try except' is finished\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf2ee9f9",
   "metadata": {},
   "source": [
    "## 引发异常\n",
    "作为 Python 开发人员，您可以选择在条件发生时抛出异常。\n",
    "\n",
    "要抛出（或引发）异常，请使用 raise 关键字。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c0b6d701",
   "metadata": {},
   "outputs": [
    {
     "ename": "Exception",
     "evalue": "Sorry, no numbers below zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mException\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-33-2edc57024fbc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m<\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m   \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Sorry, no numbers below zero\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mException\u001b[0m: Sorry, no numbers below zero"
     ]
    }
   ],
   "source": [
    "x = -1\n",
    "\n",
    "if x < 0:\n",
    "  raise Exception(\"Sorry, no numbers below zero\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d3bee288",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "Only integers are allowed",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-34-bc91768a6271>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m   \u001b[1;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Only integers are allowed\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: Only integers are allowed"
     ]
    }
   ],
   "source": [
    "x = \"hello\"\n",
    "\n",
    "if not type(x) is int:\n",
    "  raise TypeError(\"Only integers are allowed\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
